﻿namespace Huayou.Utils
{
    using System;
    using System.Text;
    using System.Text.RegularExpressions;

    public class StringUtils
    {
        private static Random _rnd = null;
        private static object _rndLock = new object();
        public const string RANDOM_STRING_SOURCE = "0123456789abcdefghijklmnopqrstuvwxyz";

        public static string Drop(string src, string pattern)
        {
            return Replace(src, pattern, "");
        }

        public static string DropHtmlTag(string content)
        {
            return Drop(content, @"<[^\>]*>");
        }

        public static string DropHtmlTag(string content, string tagName)
        {
            return DropIgnoreCase(content, "<[/]{0,1}" + tagName + @"[^\>]*\>");
        }

        public static string DropIgnoreCase(string src, string pattern)
        {
            return ReplaceIgnoreCase(src, pattern, "");
        }

        public static string EscapeHtml(string src)
        {
            if (src == null)
            {
                return null;
            }
            string str = src;
            return Replace(Replace(str, ">", "&gt;"), "<", "&lt;");
        }

        public static string GetRandomString(int num)
        {
            return GetRandomString(num, "0123456789abcdefghijklmnopqrstuvwxyz");
        }

        public static string GetRandomString(int num, string src)
        {
            if ((src == null) || (src.Length == 0))
            {
                return GetRandomString(num, "0123456789abcdefghijklmnopqrstuvwxyz");
            }
            lock (_rndLock)
            {
                if (_rnd == null)
                {
                    _rnd = new Random();
                }
            }
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < num; i++)
            {
                builder.Append(src.Substring(_rnd.Next(src.Length), 1));
            }
            return builder.ToString();
        }

        public static bool IsNumeric(string inputData)
        {
            Regex regex = new Regex(@"^\d+$");
            return regex.Match(inputData).Success;
        }

        public static string Replace(string src, string pattern, string replacement)
        {
            return Replace(src, pattern, replacement, RegexOptions.None);
        }

        public static string Replace(string src, string pattern, string replacement, RegexOptions options)
        {
            Regex regex = new Regex(pattern, options | RegexOptions.Compiled);
            return regex.Replace(src, replacement);
        }

        public static string ReplaceIgnoreCase(string src, string pattern, string replacement)
        {
            return Replace(src, pattern, replacement, RegexOptions.IgnoreCase);
        }

        public static string ToHtml(string str)
        {
            if ((str == null) || str.Equals(""))
            {
                return str;
            }
            StringBuilder builder = new StringBuilder(str);
            builder.Replace("&", "&amp;");
            builder.Replace("<", "&lt;");
            builder.Replace(">", "&gt;");
            builder.Replace("\r\n", "<br>");
            builder.Replace("\n", "<br>");
            builder.Replace("\t", "\t");
            builder.Replace(" ", "&nbsp;");
            return builder.ToString();
        }

        public static string ToSQL(string src)
        {
            if (src == null)
            {
                return null;
            }
            return Replace(src, "'", "''");
        }

        public static string ToTxt(string str)
        {
            if ((str == null) || str.Equals(""))
            {
                return str;
            }
            StringBuilder builder = new StringBuilder(str);
            builder.Replace("&nbsp;", " ");
            builder.Replace("<br>", "\r\n");
            builder.Replace("&lt;", "<");
            builder.Replace("&gt;", ">");
            builder.Replace("&amp;", "&");
            return builder.ToString();
        }
    }
}

